package cn.com.czpb.client.module.subscribe.fragment;

import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import cn.com.czpb.client.R;
import cn.com.czpb.client.app.BaseFragment;
import cn.com.czpb.client.app.MyActivitymanager;
import cn.com.czpb.client.model.news.NewsBean;
import cn.com.czpb.client.model.subscribe.FocusUserDetailBean;
import cn.com.czpb.client.model.subscribe.WriterListEntity;
import cn.com.czpb.client.module.news.adapter.NewsItemListViewAdapterNew;
import cn.com.czpb.client.module.subscribe.RefreshSubScribeListListener;
import cn.com.czpb.client.net.Factory;

import java.util.HashMap;
import java.util.List;

import butterknife.BindView;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action1;
import rx.schedulers.Schedulers;

/**
 * Created by 猪猪侠 on 2017/8/15.
 */

public class SubscribeNewsListFragment extends BaseFragment implements RefreshSubScribeListListener, BaseQuickAdapter.OnItemChildClickListener {
    @BindView(R.id.rv_list_id)
    RecyclerView rvListId;
    private NewsItemListViewAdapterNew adapter;

    @Override
    public int setMyContentView() {
        return R.layout.fragment_subscribe_newslist;
    }

    @Override
    public void initView() {
        LinearLayoutManager manager = new LinearLayoutManager(activity);
        manager.setOrientation(LinearLayoutManager.VERTICAL);
        rvListId.setLayoutManager(manager);
        adapter = new NewsItemListViewAdapterNew(activity,null,false);
        rvListId.setAdapter(adapter);
        adapter.setOnItemChildClickListener(this);
    }

    @Override
    public void initData() {

    }

    @Override
    public void refresh(FocusUserDetailBean bean) {
        getNewsList(bean.getTid());
    }

    private int page = 1;
    private int pageSize = 15;

    private void getNewsList(String tid) {
        HashMap<String, String> map = new HashMap<>();
        map.put("tid", tid);
        map.put("Page", page + "");
        map.put("PageSize", pageSize + "");
        Factory.provideHttpService().writerNewsList(map)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Action1<WriterListEntity>() {
                    @Override
                    public void call(WriterListEntity writerListEntity) {
                        setData(writerListEntity.data.getList());
                    }
                }, new Action1<Throwable>() {
                    @Override
                    public void call(Throwable throwable) {

                    }
                });

    }


    private void setData(List<NewsBean> list) {
        adapter.addData(list);
    }

    @Override
    public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
       NewsBean newsBean = (NewsBean) adapter.getItem(position);
        switch (view.getId()){
            case R.id.newsItem_root:
                MyActivitymanager.pushActivity(activity, newsBean,"", "newsitem",newsBean.getRvalue());
                break;
        }
    }
}
